Include a header file required for build on mac 10.4
[supercollider.git] / Help / Tutorials / Getting-Started / Mix it Up.html
bloba5767481534e3943d158577b2f9f716981ff893c
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="949.46">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000bf}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica; min-height: 17.0px}
19 span.s1 {color: #0000bf}
20 span.s2 {color: #000000}
21 span.s3 {text-decoration: underline}
22 span.s4 {font: 12.0px Helvetica; color: #0000bf}
23 span.s5 {font: 12.0px Helvetica}
24 span.s6 {color: #0000bf}
25 span.Apple-tab-span {white-space:pre}
26 </style>
27 </head>
28 <body>
29 <p class="p1"><b>Mix it Up</b></p>
30 <p class="p2"><br></p>
31 <p class="p3">We've already seen that multiplication changes the level of something, but what about mixing UGens together? This turns out to be equally simple. All we need is addition:</p>
32 <p class="p2"><br></p>
33 <p class="p4"><span class="Apple-tab-span"> </span>{ <span class="s1">PinkNoise</span>.ar(0.2) + <span class="s1">SinOsc</span>.ar(440, 0, 0.2) + <span class="s1">Saw</span>.ar(660, 0.2) }.play;</p>
34 <p class="p2"><br></p>
35 <p class="p3">Saw is another type of oscillator, with a waveform that looks like a sawtooth. Note that we use a low value for mul, thus ensuring that the final output will be between -1 and 1, and not clip.</p>
36 <p class="p2"><br></p>
37 <p class="p3">There's another handy class called Mix, which will mix an array of channels down to a single channel or an array of arrays of channels down to a single array of channels. Watch the post window to see Mix's results.<span class="Apple-converted-space"> </span></p>
38 <p class="p2"><br></p>
39 <p class="p5"><span class="s2"><span class="Apple-tab-span"> </span></span>// one channel</p>
40 <p class="p4"><span class="Apple-tab-span"> </span>{ <span class="s1">Mix</span>.new([<span class="s1">SinOsc</span>.ar(440, 0, 0.2), <span class="s1">Saw</span>.ar(660, 0.2)]).postln }.play;</p>
41 <p class="p6"><span class="Apple-tab-span"> </span></p>
42 <p class="p5"><span class="s2"><span class="Apple-tab-span"> </span></span>// combine two stereo arrays</p>
43 <p class="p4"><span class="Apple-tab-span"> </span>(</p>
44 <p class="p4"><span class="Apple-tab-span"> </span>{<span class="Apple-converted-space"> </span></p>
45 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">var</span> a, b;</p>
46 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a = [<span class="s1">SinOsc</span>.ar(440, 0, 0.2), <span class="s1">Saw</span>.ar(662, 0.2)];</p>
47 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>b = [<span class="s1">SinOsc</span>.ar(442, 0, 0.2), <span class="s1">Saw</span>.ar(660, 0.2)];</p>
48 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">Mix</span>([a, b]).postln;</p>
49 <p class="p4"><span class="Apple-tab-span"> </span>}.play;</p>
50 <p class="p4"><span class="Apple-tab-span"> </span>)</p>
51 <p class="p2"><br></p>
52 <p class="p3">In the first case we get a 'BinaryOpUGen' (in this case this means the two UGens added together), and in the second we get an Array of two BinaryOpUGens.</p>
53 <p class="p2"><br></p>
54 <p class="p3">Note that in the first example we use Mix.new(...), but in the second we use Mix(...). The latter is a shorthand for the former. 'new' is the most common class method for creating a new object. In some cases objects have more than one class method for creating objects, such as the 'ar' and 'kr' methods of UGens. (Mix, however, is actually<span class="Apple-converted-space">  </span>just a 'convenience' class, and doesn't actually create Mix objects, it just returns the results of its summing, either a BinaryOpUGen or an Array of them.)</p>
55 <p class="p2"><br></p>
56 <p class="p3">Mix also has another class method called fill, which takes two arguments. The first is a number, which determines how many times the second argument, a Function, will be evaluated. The results of the evaluations will be summed. Confusing? Take a look at the following example:</p>
57 <p class="p2"><br></p>
58 <p class="p4"><span class="Apple-tab-span"> </span>(</p>
59 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">var</span> n = 8;</p>
60 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>{ <span class="s1">Mix</span>.fill(n, { <span class="s1">SinOsc</span>.ar(500 + 500.0.rand, 0, 1 / n) })<span class="Apple-converted-space">  </span>}.play;</p>
61 <p class="p4"><span class="Apple-tab-span"> </span>)</p>
62 <p class="p6"><span class="Apple-tab-span"> </span></p>
63 <p class="p3">The Function will be evaluated n times, each time creating a SinOsc with a random frequency from 500 to 1000 Hz (500 plus a random number between 0 and 500). The mul arg of each SinOsc is set to 1 / n, thus ensuring that the total amplitude will not go outside -1 and 1. By simply changing the value of n, you can have vastly different numbers of SinOscs! (Try it!) This sort of approach makes this code extremely flexible and reusable.</p>
64 <p class="p2"><br></p>
65 <p class="p3">Each time the Function is evaluated it is passed the number of times evaluated so far as an argument. So if 'n' is 8 the Function will be passed values from 0 to 7, in sequence, counting up. By declaring an argument within our Function we can use this value.</p>
66 <p class="p2"><br></p>
67 <p class="p5"><span class="s2"><span class="Apple-tab-span"> </span></span>// Look at the post window for frequencies and indices</p>
68 <p class="p4"><span class="Apple-tab-span"> </span>(</p>
69 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">var</span> n = 8;</p>
70 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>{<span class="Apple-converted-space"> </span></p>
71 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">Mix</span>.fill(n, { <span class="s1">arg</span> index;<span class="Apple-converted-space"> </span></p>
72 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">var</span> freq;</p>
73 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>index.postln;</p>
74 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>freq = 440 + index;</p>
75 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>freq.postln;</p>
76 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s1">SinOsc</span>.ar(freq , 0, 1 / n)<span class="Apple-converted-space"> </span></p>
77 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>})</p>
78 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>}.play;</p>
79 <p class="p4"><span class="Apple-tab-span"> </span>)</p>
80 <p class="p2"><br></p>
81 <p class="p3">By combining addition and multiplication (or indeed almost any mathematical procedure you could imagine!) with the use of classes like Mix, we have the tools we need to combine multichannel sources of sound into complex mixes and submixes.</p>
82 <p class="p2"><br></p>
83 <p class="p3">For more information see:<b><span class="Apple-converted-space"> </span></b></p>
84 <p class="p2"><br></p>
85 <p class="p7"><span class="s3"><a href="../../UGens/Multichannel/Mix.html">Mix</a></span><span class="s2"><b> </b><a href="../../UGens/Algebraic/BinaryOpUGen.html"><span class="s4">BinaryOpUGen</span></a><b> </b><a href="../../Language/Operators.html"><span class="s5">Operators</span></a><b> </b><a href="../../Language/Syntax-Shortcuts.html"><span class="s5">Syntax-Shortcuts</span></a></span></p>
86 <p class="p2"><br></p>
87 <p class="p8"><b>Suggested Exercise:</b></p>
88 <p class="p9"><br></p>
89 <p class="p3">Experiment with altering the Functions in the text above. For instance try changing the frequencies of the SinOsc, or making multi-channel versions of things.</p>
90 <p class="p2"><br></p>
91 <p class="p3">____________________</p>
92 <p class="p2"><br></p>
93 <p class="p3">This document is part of the tutorial <b>Getting Started With SuperCollider</b>.</p>
94 <p class="p2"><br></p>
95 <p class="p3">Click here to go on to the next section: <a href="Scoping and Plotting.html"><span class="s6">Scoping and Plotting</span></a></p>
96 <p class="p2"><br></p>
97 <p class="p3">Click here to return to the table of Contents: <a href="Getting Started With SC.html"><span class="s6">Getting Started With SC</span></a></p>
98 </body>
99 </html>